package com.xuchen.zhenyang.controller.statistics;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.Goods;
import com.xuchen.zhenyang.entity.User;
import com.xuchen.zhenyang.enums.GoodsTypeEnum;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.enums.UserTypeEnums;
import com.xuchen.zhenyang.model.statistics.StatisticsDeliveryQueryModel;
import com.xuchen.zhenyang.model.statistics.StatisticsGoodsSaleModel;
import com.xuchen.zhenyang.model.statistics.StatisticsUserGoodsModel;
import com.xuchen.zhenyang.model.statistics.StatisticsUserGoodsQuery;
import com.xuchen.zhenyang.service.GoodsService;
import com.xuchen.zhenyang.service.StatisticsService;
import com.xuchen.zhenyang.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("statistics")
@RequiresPermissions("statistics")
public class StatisticsController extends BaseController {

    @Autowired
    StatisticsService statisticsService;
    @Autowired
    UserService userService;
    @Autowired
    GoodsService goodsService;


    @GetMapping("/main")
    String index(HttpServletRequest request) {
        request.setAttribute("goodsType", GoodsTypeEnum.getMap());
        return "main";
    }

    /**
     * 商品每月销售统计
     *
     * @param monthTime yyyyMM
     * @param goodsType 增加goodsType参数，为了避免商品种类过多，影响美观
     * @return
     */
    @GetMapping("getMonthGoodsSale")
    @ResponseBody
    @RequestLog
    Result getMonthGoodsSale(Integer monthTime, Integer goodsType) {
        List<StatisticsGoodsSaleModel> list = statisticsService.getMonthGoodsSale(monthTime, goodsType);
        return Result.success(list);
    }

    @GetMapping("/refresh")
    @ResponseBody
    @RequestLog
    Result refresh(Integer type) {
        switch (type) {
            case 1:
                statisticsService.refreshGoodsSale();
                return Result.success("刷新【商品销售】统计数据成功");
            case 2:
                statisticsService.refreshUserOrder();
                return Result.success("刷新【用户订单】统计数据成功");
            case 3:
                statisticsService.refreshUserDebt();
                return Result.success("刷新【用户账单】统计数据成功");
            default:
                return Result.fail();
        }
    }


    @GetMapping("/sumDelivery")
    String sumDeilivery(HttpServletRequest request) {
        List<User> deliverList = userService.selectList(new EntityWrapper<User>()
                .eq("user_type", UserTypeEnums.OTHER.getId()).or().eq("user_type", UserTypeEnums.DELIVER.getId())
                .orderBy("user_name"));
        List<User> customerList = userService.selectList(new EntityWrapper<User>().orderBy("user_name"));
        request.setAttribute("customerList", customerList);
        request.setAttribute("deliverList", deliverList);
        return "statistics/sum/delivery/sum-delivery";
    }

    @GetMapping("/getSumDelivery")
    @ResponseBody
    @RequestLog
    Result getSumDelivery(StatisticsDeliveryQueryModel queryModel) {
        BigDecimal sumDelivery = statisticsService.getSumDelivery(queryModel);
        return Result.success(sumDelivery);
    }


    /**
     * 用户一个时间段的商品信息
     */
    @GetMapping("/userGoods")
    String userGoods(HttpServletRequest request) {
        List<Goods> list = goodsService.selectList(new EntityWrapper<>());
        request.setAttribute("goodsList", list);
        List<User> customerList = userService.selectList(new EntityWrapper<User>().orderBy("user_name"));
        request.setAttribute("customerList", customerList);
        return "statistics/user/goods/statistics-user-goods-list";
    }

    /**
     * 用户一个时间段的商品信息
     */
    @GetMapping("/getUserGoods")
    @ResponseBody
    @RequestLog
    Result getUserGoods(StatisticsUserGoodsQuery queryModel) {
        queryModel.startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.orderBy(queryModel.getPageField(), "asc".equals(queryModel.getPageOrder()));
        List<StatisticsUserGoodsModel> list = statisticsService.getUserGoods(queryModel);
        return Result.success(PageHelper.freeTotal(), list);
    }

}
